//
//  YearMonthDayDatePickerView.swift
//  UI Single
//
//  Created by vivi on 6/5/16.
//  Copyright © 2016 vivi. All rights reserved.
//

import UIKit

class YearMonthDayDatePickerView: CustomDatePickerBaseView {
    
    var year: Int {
        get {
            return Int(self.tableView3.currentText)!
        }
        set {
            setYearTo(newValue, animated: false)
        }
    }
    
    var month: Int {
        get {
            let currentMonthText = self.tableView1.currentText
            return Int(currentMonthText.substringToIndex(currentMonthText.startIndex.advancedBy(2)))!
        }
        set {
            setMonthTo(newValue, animated: false)
        }
    }
    
    var day: Int {
        get {
            let currentDayText = self.tableView2.currentText
            return Int(currentDayText.substringToIndex(currentDayText.startIndex.advancedBy(2)))!
        }
        set {
            setDayTo(newValue, animated: false)
        }
    }
    
    override func configureInit() {
        super.configureInit()
        
        self.tableView3.texts = (1970...2037).map{String($0)}
        self.tableView1.texts = (1...12).map{String.init(format: "%02d月", $0)}
        self.tableView2.texts = (1...31).map{String.init(format: "%02d日", $0)}
    }
    
    //MARK: - 设置年月
    
    func setYearTo(year: Int, animated: Bool) {
        self.tableView3.setCurrentTextTo(String(year), animated: animated)
    }
    
    func setMonthTo(month:Int, animated: Bool) {
        self.tableView1.setCurrentTextTo(String(format: "%02d月", month), animated: animated)
    }
    
    func setDayTo(day: Int, animated: Bool) {
        self.tableView2.setCurrentTextTo(String(format: "%02d日", day ), animated: animated)
    }
    
    //MARK: - Handler
    
    //MARK: - Delegate
    override func datePickerTableViewDidSelectNew(datePickerTableView: DatePickerTableView) {
        switch datePickerTableView.tag {
        case 0:  //选择年份
            fallthrough
        case 1:  //选择月份
            //MARK: - FIXME -> 加入调节日的代码
            let totalDay =  NSDate.totalDaysInTheMonth(NSDate(year: year, month: month, day: 1))
            //print("\(year) \(month) \(day)")
            self.tableView2.maxNumToShow = totalDay
            fallthrough
        case 2:  //选择日
            break
        default:
            break
        }
    }
}
